package com.lemon.tuvi2015;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;

import com.lemon.tuvi2015.adapter.VankhanAdapter;
import com.lemon.tuvi2015.object.DetailsObject;
import com.lemon.tuvi2015.object.GlobalObject;
import com.lemon.tuvi2015.util.BaseEngine;
import com.lemon.tuvi2015.util.CMD;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;
import com.lemon.tuvi2015.view.CustomActionbar;

public class VankhanActivity extends Activity {
	CustomActionbar actionbar;
	String ID;
	ArrayList<GlobalObject> listZodiac;
	ListView listView;
	ProgressBar progressBar;
	View main;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vankhan_layout);
		initView();

		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(TagName.KEY))
			ID = (String) extras.getString(TagName.KEY);
		if (ID != null && !ID.equals(""))
			new AsyncTaskParseJson().execute("");
		initActionbar();
	}

	void initView() {
		main=findViewById(R.id.main);
		progressBar=(ProgressBar) findViewById(R.id.progressBar);
		listView = (ListView) findViewById(R.id.listView);
		listView.setEmptyView(progressBar);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(VankhanActivity.this, VankhanListActivity.class);
				intent.putExtra(TagName.KEY, listZodiac.get(position));
				startActivity(intent);
//				new AsyncTaskParseJson2().execute(listZodiac.get(position).getID());
			}
		});
	}

	void initActionbar() {
		actionbar = (CustomActionbar) findViewById(R.id.header);
		OnClickListener listenerBack = new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		};
		OnClickListener listenerShare = new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				BaseEngine.shareBitmap(VankhanActivity.this,  BaseEngine.ConvertViewtoBitmap(VankhanActivity.this, main));
			}
		};
		actionbar.setActionbar(R.string.home_menu_vankhan, listenerBack, listenerShare);
	}

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
		JSONObject jsonObject;

		@Override
		protected void onPreExecute() {
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.getJson(CMD.HOME_ITEM_DETAILS, ID);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result != null) {
				BaseEngine.showLogformLongString("", result);
				try {
					if(!JsonParser.readStatus(result).equals("")){
						listZodiac = JsonParser.readlistZodiac(result);
						if (listZodiac != null && listZodiac.size() > 0) {
							listView.setAdapter(new VankhanAdapter(getApplicationContext(), listZodiac));
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

//	public class AsyncTaskParseJson2 extends AsyncTask<String, String, String> {
//		@Override
//		protected void onPreExecute() {
//		}
//
//		@Override
//		protected String doInBackground(String... arg0) {
//			String str = JsonParser.getJson(CMD.HOME_ITEM_DETAILS, arg0[0]);
//			return str;
//		}
//
//		@Override
//		protected void onPostExecute(String result) {
//			if (result != null) {
//				BaseEngine.showLogformLongString("", result);
////				try {
////					ArrayList<DetailsObject> arrayList = JsonParser.readDetails(result);
////					Intent intent = new Intent(VankhanActivity.this, DetailsActivity.class);
////					intent.putExtra(TagName.TITLE, "Nốt ruồi: "+editText.getText().toString());
////					intent.putExtra(TagName.KEY, result);
////					startActivity(intent);
////					if (arrayList != null && arrayList.size() > 0) {
////						showContent(arrayList.get(0));
////					}
////				} catch (JSONException e) {
////					e.printStackTrace();
////				}
//			}
//		}
//	}
}
